from django.contrib import admin
from django.utils.html import format_html
from django import forms

from .models import Auth,Order,Cart
import json
from goods.models import GoodsInfo
# Register your models here.

FINSHED = '<span style="font-size:larger;font-weight:bolder;background:#ddffdd;color:black">{}</span>'
NOT_FINSHED =  '<span style="font-size:larger;font-weight:bolder;background:#ddffdd;color:red">{}</span>'


@admin.register(Auth)
class UserAdmin(admin.ModelAdmin):
    # def get_queryset(self, request):
    #     qs = super().get_queryset(request)
    #     print(request.Auth.cart)
    pass


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin,object):

    def showPaymentFunc(self,object):
        res = object.paymentStatus

        if res == "1":
            return format_html(FINSHED,"已付款")
        return format_html(NOT_FINSHED,"待付款")

    def DeliveryStatusFunc(self,object):
        res = object.paymentStatus
        print(res)
        if res == "1":
            if object.DeliveryStatus == "1":
                return format_html(FINSHED, "已发货")
            object.DeliveryStatus = "0"
            object.save()
            return format_html(NOT_FINSHED,"待发货")
        return format_html(NOT_FINSHED, "----------")

    def harvestStatusFunc(self,object):
        res = object.DeliveryStatus
        if res == "1":
            if object.harvestStatus == "1":
                return format_html(FINSHED, "已收货")
            object.harvestStatus = "0"
            object.save()
            return format_html(NOT_FINSHED, "待收货")
        return format_html(NOT_FINSHED, "----------")

    def paymentModeFunc(self,object):
        res = object.paymentMode
        if res == "1":
            return format_html(FINSHED,"线下付款")
        elif res == "2":
            return format_html(FINSHED,"线上付款")
        return format_html(NOT_FINSHED,"----------")

    fields = [("orderNumber","userID","showAddress"),
              "goodsInfo",
              ("paymentTime","paymentStatus","paymentMode"),
              "buyerAnnocate",
              ("DeliveryStatus","harvestStatus"),
              "joinTime"]
    showPaymentFunc.short_description = "付款状态"
    DeliveryStatusFunc.short_description = "发货状态"
    harvestStatusFunc.short_description = "收获状态"
    paymentModeFunc.short_description = "付款方式"
    list_display = ["orderNumber","userID","paymentTime","joinTime","showPaymentFunc","paymentModeFunc","DeliveryStatusFunc","harvestStatusFunc"]


    def get_order_number(self):
        import random,time
        from datetime import datetime
        res1 = str(random.randint(0, 10) * 10)
        res2 = str(random.randint(0, 10) * 10)
        res3 = str(int(time.time()))
        return res1 + res2 + res3




class CartFormsAdmin(forms.ModelForm):
    goodsInfo1 = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple, queryset=GoodsInfo.objects.all())

@admin.register(Cart)
class CartAdmin(admin.ModelAdmin):


    pass





